package com.bjc.utils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.sql.*;
import java.util.Iterator;
import java.util.Map;

/**
 * @program: servlet
 * @description:
 * @author: bjc
 * @create: 2021-08-19 19:58
 */
public class JdbcUtil {

    final String URL = "jdbc:mysql:///user_db?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false";
    final String USERNAME = "root";
    final String PASSWORD = "123456";
    PreparedStatement ps = null;
    Connection con = null;

    //将jar包中driver实现类加载到JVM中
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过全局作用域对象得到Connection
     * @param request
     * @return
     */
    public Connection getCon(HttpServletRequest request){
        //1.通过请求对象，得到全局作用域对象
        ServletContext context = request.getServletContext();
        //2.从全局作用域对象的到map
        Map map = (Map) context.getAttribute("key1");
        //3.从map得到一个处于空闲状态Connection
        Iterator iterator = map.keySet().iterator();
        while (iterator.hasNext()){
            con = (Connection)iterator.next();
            boolean flag = (boolean) map.get(con);
            if (flag){
                map.put(con,false);
                break;
            }
        }
        return con;
    }

    public PreparedStatement createStatement(String sql,HttpServletRequest request) {

        try {
            ps = getCon(request).prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ps;
    }

    public void close(HttpServletRequest request) {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        ServletContext context = request.getServletContext();
        Map map = (Map) context.getAttribute("key1");
        map.put(con,true);
    }

    //封装连接通道创建细节
    public Connection getCon() {

        try {
            con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    //封装交通工具创建细节
    public PreparedStatement createStatement(String sql) {

        try {
            ps = getCon().prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ps;
    }

    // ps与con销毁细节 insert,update,delete
    public void close() {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //select ps,con,rs
    public void close(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        close();
    }
}
